package com.jit.app;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.bumptech.glide.Glide;
import com.jit.app.model.AppInfoModel;
import com.jit.app.model.Constants;
import com.jit.app.util.JSONResult;
import java.util.HashMap;
import java.util.Map;

public class PublicCommentActivity extends AppCompatActivity {

    public static final int PUBLIC_RESULT_CODE = 200;
    public static final int CANCEL_RESULT_CODE = 400;

    AppInfoModel model = new AppInfoModel();
    Handler handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_publiccomment);

        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("发表评价");
        this.setSupportActionBar(toolbar);

        //导航图标
        toolbar.setNavigationIcon(R.drawable.cancel);
        toolbar.setNavigationOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setResult(CANCEL_RESULT_CODE);
                        finish();
                    }
                }
        );

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch(msg.what){
                    case Constants.MessageWhat.COMMENT_SAVE_SUCCESS:
                        JSONResult jsonResult = (JSONResult) msg.obj;
                        if(jsonResult.getCode() == Constants.ResultCode.SUCCESS){
                            Intent intent = new Intent();
                            intent.putExtra("commentInfo", (com.alibaba.fastjson.JSONObject)jsonResult.getData().get("appComment"));
                            setResult(PUBLIC_RESULT_CODE,intent);
                            finish();
                        }
                        break;
                }
                return false;
            }
        });

        initView();

    }

    ImageView appLogoImageView;
    RatingBar myscoreRatingBar;
    TextView myinfoTextView;
    EditText contentEditText;

    Map<Integer,String> scoreInfo = new HashMap<>();

    Map appInfo;
    Map userInfo;

    private void initView(){
        appLogoImageView = findViewById(R.id.iv_applogo);
        myscoreRatingBar = findViewById(R.id.rb_myscore);
        myinfoTextView = findViewById(R.id.tv_info);
        contentEditText = findViewById(R.id.et_content);

        userInfo = (Map)getIntent().getSerializableExtra("userInfo");
        appInfo = (Map) getIntent().getSerializableExtra("appInfo");
        Glide.with(this).load(Constants.HOST.FILE_HOST+(String)appInfo.get("logoPicPath")).into(appLogoImageView);

        scoreInfo.put(0,"差评");
        scoreInfo.put(1,"不满意");
        scoreInfo.put(2,"不太满意");
        scoreInfo.put(3,"基本满意");
        scoreInfo.put(4,"满意");
        scoreInfo.put(5,"非常满意");

        myscoreRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                myinfoTextView.setText(scoreInfo.get((int)rating));
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.add(0,1,1,"发表");
        Button button = new Button(this);
        button.setText("发表");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float myscore = myscoreRatingBar.getRating();
                String content = contentEditText.getText().toString();

                //调用后端接口，保存评论
                model.postComment(handler,(Integer)appInfo.get("id"),(Integer)userInfo.get("id"),(int)myscore,content);

            }
        });
        menuItem.setActionView(button);
        menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        return super.onCreateOptionsMenu(menu);
    }
}
